package com.ruoyi.merchant.controller;

import cn.hutool.core.lang.Validator;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.merchant.domain.ImsVehSupplierUnit;
import com.ruoyi.merchant.service.IImsVehSupplierUnitService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商单位Controller
 * 
 * @author ruoyi
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/supplerunit/vehsupplierunit")
public class ImsVehSupplierUnitController extends BaseController
{
    @Autowired
    private IImsVehSupplierUnitService imsVehSupplierUnitService;

    /**
     * 查询供应商单位列表
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(ImsVehSupplierUnit imsVehSupplierUnit)
    {
        startPage();
        List<ImsVehSupplierUnit> list = imsVehSupplierUnitService.selectImsVehSupplierUnitList(imsVehSupplierUnit);
        return getDataTable(list);
    }

    /**
     * 导出供应商单位列表
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:export')")
    @Log(title = "供应商单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Anonymous
    public void export(HttpServletResponse response, ImsVehSupplierUnit imsVehSupplierUnit)
    {
        List<ImsVehSupplierUnit> list = imsVehSupplierUnitService.selectImsVehSupplierUnitList(imsVehSupplierUnit);
        ExcelUtil<ImsVehSupplierUnit> util = new ExcelUtil<ImsVehSupplierUnit>(ImsVehSupplierUnit.class);
        util.exportExcel(response, list, "供应商单位数据");
    }

    /**
     * 获取供应商单位详细信息
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:query')")
    @GetMapping(value = "/{id}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imsVehSupplierUnitService.selectImsVehSupplierUnitById(id));
    }

    /**
     * 新增供应商单位
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:add')")
    @Log(title = "供应商单位", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous
    public AjaxResult add(@RequestBody ImsVehSupplierUnit imsVehSupplierUnit)
    {
        AjaxResult ajax=AjaxResult.success();
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehSupplierUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getPersonEmail())) {
            if (!Validator.isEmail(imsVehSupplierUnit.getPersonEmail())) {
                return error("邮箱不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getComContactIdCard())) {
            if (!Validator.isCitizenId(imsVehSupplierUnit.getComContactIdCard())) {
                return error("法人身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getComEmail())) {
            if (!Validator.isEmail(imsVehSupplierUnit.getComEmail())) {
                return error("企业邮箱不正确！");
            }
        }
        imsVehSupplierUnit.setAddTime(DateUtils.getNowDate());
        imsVehSupplierUnitService.insertImsVehSupplierUnit(imsVehSupplierUnit);
        ajax.put("supplierUnitId",imsVehSupplierUnit.getId());
        return ajax;
    }

    /**
     * 修改供应商单位
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:edit')")
    @Log(title = "供应商单位", businessType = BusinessType.UPDATE)
    @PutMapping
    @Anonymous
    public AjaxResult edit(@RequestBody ImsVehSupplierUnit imsVehSupplierUnit)
    {
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getPersonTel())) {
            if (!Validator.isMobile(imsVehSupplierUnit.getPersonTel())) {
                return error("手机号不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getPersonEmail())) {
            if (!Validator.isEmail(imsVehSupplierUnit.getPersonEmail())) {
                return error("邮箱不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getComContactIdCard())) {
            if (!Validator.isCitizenId(imsVehSupplierUnit.getComContactIdCard())) {
                return error("法人身份证号码不正确！");
            }
        }
        if(ObjectUtils.isNotEmpty(imsVehSupplierUnit.getComEmail())) {
            if (!Validator.isEmail(imsVehSupplierUnit.getComEmail())) {
                return error("企业邮箱不正确！");
            }
        }
        return toAjax(imsVehSupplierUnitService.updateImsVehSupplierUnit(imsVehSupplierUnit));
    }

    /**
     * 删除供应商单位
     */
    //@PreAuthorize("@ss.hasPermi('supplerunit:vehsupplierunit:remove')")
    @Log(title = "供应商单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @Anonymous
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imsVehSupplierUnitService.deleteImsVehSupplierUnitByIds(ids));
    }
}
